package com.xuelang.pipeline.domain.application.service.impl;

import com.xuelang.pipeline.client.application.dto.ApplicationQuery;
import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.domain.application.Application;
import com.xuelang.pipeline.domain.application.repository.ApplicationRepository;
import com.xuelang.pipeline.domain.application.service.ApplicationDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 应用领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ApplicationDomainServiceImpl implements ApplicationDomainService {

    @Autowired
    private ApplicationRepository applicationRepository;

    @Override
    public PageRecord<Application> pageList(long currentPage, long pageSize, ApplicationQuery applicationQuery) {
        return applicationRepository.pageList(currentPage, pageSize, applicationQuery);
    }

    @Override
    public List<Application> list(ApplicationQuery applicationQuery) {
        return applicationRepository.list(applicationQuery);
    }


    @Override
    public Boolean save(Application application) {
        if(existsCode(application.getCode())) {
            throw new BizException("应用编码已存在");
        }
        if(existsName(application.getName(), application.getCode())) {
            throw new BizException("应用名称已存在");
        }
        return applicationRepository.save(application);
    }

    @Override
    public Boolean update(Application application) {
        // 更新时，名称不能重复，摒弃当前应用的名称。
        if(existsName(application.getName(), application.getCode())) {
            throw new BizException("应用名称已存在");
        }
        return applicationRepository.update(application);
    }

    @Override
    public Boolean delete(Application application) {
        return applicationRepository.delete(application);
    }

    @Override
    public Boolean existsCode(String applicationCode) {
        return applicationRepository.existsCode(applicationCode);
    }

    @Override
    public Boolean existsName(String applicationName, String applicationCode) {
        return applicationRepository.existsName(applicationName, applicationCode);
    }

    @Override
    public Application query(String applicationCode) {
        return applicationRepository.query(applicationCode);
    }

    @Override
    public List<Application> list(String userId, ApplicationQuery query) {
        return applicationRepository.list(userId, query);
    }
}
